package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.CategoryBrandService;
import com.atguigu.spzx.model.pojo.product.CategoryBrand;
import com.atguigu.spzx.model.request.product.CategoryBrandRequest;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.product.CategoryBrandVo;
import com.atguigu.spzx.utils.DYLAssert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/product/categoryBrand")
@Tag(name = "分类品牌管理")
@CrossOrigin(allowCredentials = "true", originPatterns = "*", allowedHeaders = "*")
public class CategoryBrandController {
    @Autowired
    private CategoryBrandService categoryBrandService;

    @GetMapping("/findPage/{pageNum}/{pageSize}")
    @Operation(summary = "分页查询")
    public Result<Page<CategoryBrandVo>> findPage(@PathVariable("pageNum") Integer pageNum,
                                                  @PathVariable("pageSize") Integer pageSize,
                                                  CategoryBrandRequest categoryBrandRequest) {
        Page<CategoryBrand> page = new Page<>(pageNum, pageSize);
        return Result.ok(categoryBrandService.findPage(page, categoryBrandRequest));
    }

    @PostMapping("/addCategoryBrand")
    @Operation(summary = "添加品牌分类")
    public Result addCategoryBrand(@RequestBody CategoryBrandRequest categoryBrandRequest) {
        DYLAssert.isTrue(categoryBrandService.addCategoryBrand(categoryBrandRequest), "添加失败");
        return Result.ok();
    }

    @PutMapping("/updateCategoryBrand/{id}")
    @Operation(summary = "添加品牌分类")
    public Result updateCategoryBrand(@RequestBody CategoryBrandRequest categoryBrandRequest,
                                      @PathVariable("id") Long id) {
        DYLAssert.isTrue(categoryBrandService.updateCategoryBrand(categoryBrandRequest, id), "添加失败");
        return Result.ok();
    }

    @DeleteMapping("/deleteCategoryBrand/{id}")
    @Operation(summary = "添加品牌分类")
    public Result deleteCategoryBrand(@PathVariable("id") Long id) {
        DYLAssert.isTrue(categoryBrandService.removeById(id), "删除失败");
        return Result.ok();
    }
}
